<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../lib/Cookie.js"></script>
    <style>
        .box {
            position: absolute;
            top: 50;
            left: 50;
            /* width: 250px; */
            padding: 20px;
            border: 1px solid #ccc;
        }
        
        img {
            width: 230px
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="../img/cookie.png" alt="">
    </div>
</body>
<script>

    window.onload = () =>{
        let box = document.querySelector(".box");
        box.onmousedown = function(ev){
            let disX = ev.offsetX;
            let disY = ev.offsetY;
           Cookie.setItem("disX",disX);
           Cookie.setItem("disY",disY);
            // console.log(disX,disY);
            
            document.onmousemove = function(ev){
                let left = ev.pageX - disX;
                let top = ev.pageY - disY;
                if (left <= 30) {//水平临界值：吸附
                    left = 0;
                } else if (left >= (window.innerWidth - box.offsetWidth - 10)) {
                    left = window.innerWidth - box.offsetWidth;
                }
                box.style.left = left + 'px';
                box.style.top = top + 'px';
            }
            document.onmouseup = function(){
                document.onmousemove = null;
                Cookie.clear();
            }
        }
        
    }
</script>
</html>